package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import data_management.InvDBManager;
import data_management.Main;
import data_management.PermissionManager;

public class InventoryInterface extends JPanel implements IPrint, ActionListener, DocumentListener {

	public static final InventoryInterface inv = new InventoryInterface();

	private ScrollTable searchtable;
	//@ searchtable != null;
	private JTextField searchField;
	//@ searchField != null;
	private JComboBox columnFilter;
	//@ columnFilter != null;
	private JButton newItem;
	//@ newItem != null;
	private JButton newCategory;
	//@ newCategory != null;

	public void initialize() {

		setLayout(new BorderLayout());

		// Add the top panel that will contain our searching utilities
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
		add(northPanel, BorderLayout.NORTH);

		// add the new item button
		newItem = new JButton("New Item");
		newItem.addActionListener(this);
		northPanel.add(newItem);

		// add the new category button
		newCategory = new JButton("Add Category");
		newCategory.addActionListener(this);
		northPanel.add(newCategory);

		// align right
		northPanel.add(Box.createHorizontalGlue());

		// add a cool label
		JLabel searchLabel = new JLabel("Search: ");
		northPanel.add(searchLabel);

		// add the search field
		searchField = new JTextField(15);
		searchField.setMaximumSize(new Dimension(50, 25));
		searchField.setMinimumSize(new Dimension(50, 25));
		searchField.getDocument().addDocumentListener(this);
		northPanel.add(searchField);

		northPanel.add(Box.createHorizontalStrut(15));

		// Add the filter label
		JLabel filterLabel = new JLabel("Filter by: ");
		northPanel.add(filterLabel);

		// Add our filter by drop down box
		Vector<String> columns = InvDBManager.getInstance().getColumns("inventory");
		columnFilter = new JComboBox(columns);
		columnFilter.setMaximumSize(new Dimension(50, columnFilter.getMaximumSize().height));
		columnFilter.addActionListener(this);
		northPanel.add(columnFilter);

		// Make the table		
		searchtable = new ScrollTable(columns);

		add(searchtable.getPane(), BorderLayout.CENTER);

		searchtable.updateTable(InvDBManager.getInstance().searchDatabase("%", "Name"));

		setBackground(Color.red);
		setPreferredSize(new Dimension(Main.DEFAULT_WIDTH, Main.DEFAULT_HEIGHT));
	}

	public void Print() {
		// TODO Auto-generated method stub

	}

	/**
	 * Removes a category from the database.
	 * 
	 * @param key The category to remove
	 */
	/*@
	  @
	  @ requires key != null;
	  @*/
	public void removeCategory(String key) {
	}

	/**
	 * Adds a category to the database with the given name.
	 * 
	 * @param name The name of the new category
	 */
	/*@ 
	  @ requires name != null;
	  @ requires !name.trim().equals("");
	  @*/
	public void addCategory(String name) {

		InvDBManager.getInstance().addCategory(name, PermissionManager.getInstance().desktopUser);
		Vector<String> columns = InvDBManager.getInstance().getColumns("inventory");
	}

	/**
	 * Edits an item in the inventory
	 * @param key The SQL values to edit this item
	 */
	/*@ 
	  @ requires key != null;
	  @*/
	public void modifyInventory(String key) {
	}

	public void addNewItem() {

		AddItemDialog dialog = new AddItemDialog();
	}

	/**
	 * Gets a currently running instance of this class
	 */
	public static InventoryInterface getInstance() {
		return inv;
	}

	@Override
	public void actionPerformed(ActionEvent e) {	

		Object source = e.getSource();

		if (source == newItem) {
			addNewItem();
		}
		else if (source == columnFilter) {			
			performSearch();
		}
		else if (source == newCategory) {
			String name = JOptionPane.showInputDialog("Enter the name of the new category:");
			addCategory(name);
		}
	}

	/**
	 * Performs a search based on the text input by the user and updates
	 * the ScrollTable accordinly to display it.
	 */
	/*@
	 @
	 @ ensures searchtable = \old searchtable;
	 @
	 */
	public void performSearch() {
		String key = searchField.getText() + "%";
		String column = columnFilter.getSelectedItem().toString();
		ResultSet results = InvDBManager.getInstance().searchDatabase(key, column);
		searchtable.updateTable(results);
	}

	@Override
	public void changedUpdate(DocumentEvent e) {

		// do nothing
	}

	@Override
	public void insertUpdate(DocumentEvent e) {

		performSearch();
	}

	@Override
	public void removeUpdate(DocumentEvent e) {

		performSearch();
	}

}
